﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class OrtuAdd : Form
    {
        private Ortu ortu = null;
        private OrtuFind ortufind = null;

        public OrtuAdd()
        {
            InitializeComponent();
        }

        // Constructor for OrtuAdd from 
        public OrtuAdd(Ortu parent)
        {
            InitializeComponent();
            this.ortu = parent;
        }

        public OrtuAdd(OrtuFind parent)
        {
            InitializeComponent();
            this.ortufind = parent;
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSimpan_Click(object sender, EventArgs e)
        {
            if (false == checkFieldData())
            {
                MessageBox.Show("Harap isi semua data !", "Peringatan !");
                return;
            }

            DataTable dt = Database.getInstance().query("select * from Parent");
            int par_id = -1; 

            if (dt.Rows.Count == 0)
            {
                par_id = 1;
            }
            else
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string buffer = "";
                    buffer = dt.Rows[i].ItemArray[0].ToString();
                    int num = int.Parse(buffer);

                    if (num > par_id)
                        par_id = num;
                }

                par_id += (par_id + 1);
            }

            DataRow dataRow = dt.NewRow();
            dataRow["par_id"] = par_id;
            dataRow["par_name"] = this.txtNama.Text;
            dataRow["par_address"] = this.txtAlamat.Text;
            dataRow["par_job"] = this.txtPekerjaan.Text;
            dataRow["par_phone"] = this.textPhone.Text;
            dataRow["par_mobile"] = this.textMobile.Text;
            dataRow["par_email"] = this.textEmail.Text;
            dt.Rows.Add(dataRow);

            Database.getInstance().updateDB(dt);

            var result = MessageBox.Show("Tambah orang tua berhasil", "Sukses !");
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                if (this.ortufind != null)
                {
                    // Add new item in OrtuFind ListBox control
                    this.ortufind.lstNama.Items.Add(this.txtNama.Text);
                    this.ortufind.lstNama.Refresh();
                    this.ortufind.dTable = dt;
                }
                
                this.Close();
            }
        }

        private Boolean checkFieldData()
        {
            if (this.txtNama.Text == "" ||
                this.txtAlamat.Text == "" ||
                this.txtPekerjaan.Text == "" ||
                this.textPhone.Text == "" ||
                this.textMobile.Text == "" ||
                this.textEmail.Text == "")
            {
                return false;
            }
            return true;
        }
    }
}
